package com.cetc;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Classname TcpTest
 * @Description Tcp网络编程测试
 * @Date 2022/11/25 18:04
 * @Created by louzhuangxi
 */
public class TcpTest {
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            InetAddress serverInet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(serverInet, 8899);
            os = socket.getOutputStream();
            os.write("你好,我是客户端".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void server() {
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8899);
            socket = serverSocket.accept();
            is = socket.getInputStream();
            bos = new ByteArrayOutputStream();
            int len;
            byte[] data = new byte[2];
            while ((len = is.read(data)) != -1) {
                bos.write(data, 0, len);
            }
            System.out.println("服务端收到的消息为:" + bos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (socket != null) {
                    socket.close();
                }
                if (is != null) {
                    is.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (serverSocket != null) {
                    serverSocket.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 客户端给服务端发图片
     */
    @Test
    public void example01_client() {
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        try {
            InetAddress serverInet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(serverInet, 8899);
            os = socket.getOutputStream();
            File sourceFile = new File("data/1.png");
            fis = new FileInputStream(sourceFile);
            int len;
            byte[] data = new byte[1024];
            while ((len = fis.read(data)) != -1) {
                os.write(data, 0, len);
            }
            socket.shutdownOutput();
            //接收服务端的反馈
            is = socket.getInputStream();
            bos = new ByteArrayOutputStream();
            int len1;
            byte[] data1 = new byte[1024];
            while ((len1 = is.read(data1)) != -1) {
                bos.write(data1, 0, len1);
            }
            System.out.println("收到客户端的反馈为:" + bos.toString());


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (socket != null) {
                    socket.close();
                }
                if (fis != null) {
                    fis.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 客户端给服务端发图片
     */
    @Test
    public void example01_server() {
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        ServerSocket serverSocket = null;
        FileOutputStream fos = null;
        OutputStream os = null;
        try {
            serverSocket = new ServerSocket(8899);
            socket = serverSocket.accept();
            is = socket.getInputStream();
            bos = new ByteArrayOutputStream();
            int len;
            byte[] data = new byte[2];
            while ((len = is.read(data)) != -1) {
                bos.write(data, 0, len);
            }
            fos = new FileOutputStream("data/2.png");
            fos.write(bos.toByteArray());
            System.out.println("文件传输完成");
            //给客户端反馈
            os = socket.getOutputStream();
            os.write("我已经收到客户端发送的图片".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (socket != null) {
                    socket.close();
                }
                if (is != null) {
                    is.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (serverSocket != null) {
                    serverSocket.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
